#!/usr/bin/env python3
"""
验证markdown中图片和解释文本的匹配关系
"""

import re

# 读取markdown文件
with open('course/MBA工商管理回归分析教程_代码集成版.md', 'r', encoding='utf-8') as f:
    content = f.read()

# 提取图片和相关文本
image_pattern = r'!\[([^\]]+)\]\(([^\)]+)\)'
images = re.findall(image_pattern, content)

# 过滤case04图片
case04_images = [(desc, path) for desc, path in images if 'case04' in path]

print("=" * 80)
print("案例4图片和文本匹配验证")
print("=" * 80)
print(f"\n找到 {len(case04_images)} 张案例4图片\n")

# 检查每张图片
expected_images = [
    ('case04_01_value_by_position.png', '市场价值分布', '箱线图'),
    ('case04_02_log_value_distribution.png', '对数价值分布', '直方图'),
    ('case04_03_variable_importance.png', '变量重要性', '条形图'),
    ('case04_04_shap_waterfall_forward.png', '顶级前锋SHAP', '瀑布图'),
    ('case04_05_shap_waterfall_midfielder.png', '顶级中场SHAP', '瀑布图'),
    ('case04_06_shap_waterfall_young.png', '年轻潜力股SHAP', '瀑布图'),
    ('case04_07_shap_beeswarm.png', 'SHAP蜂群图', '全局解释'),
    ('case04_08_shap_dependence_winratio.png', '球队胜率依赖图', '依赖图'),
    ('case04_09_shap_dependence_age.png', '年龄依赖图', '依赖图'),
    ('case04_10_shap_dependence_goals.png', '进球效率依赖图', '依赖图'),
    ('case04_11_shap_by_position_goals.png', '位置对比-进球', '位置对比'),
    ('case04_12_shap_by_position_winratio.png', '位置对比-胜率', '位置对比'),
    ('case04_13_shap_interaction_age_position.png', '年龄位置交互', '交互效应'),
    ('case04_14_interaction_team_player.png', '球队球员交互', '交互效应'),
]

# 验证每张图片
issues = []
for i, (filename, expected_desc, chart_type) in enumerate(expected_images, 1):
    found = False
    for desc, path in case04_images:
        if filename in path:
            found = True
            print(f"✓ 图{i}: {filename}")
            print(f"  描述: {desc}")
            print(f"  类型: {chart_type}")
            
            # 检查描述是否包含关键词
            if expected_desc.lower() not in desc.lower() and desc.lower() not in expected_desc.lower():
                issues.append(f"图{i}描述可能不匹配: 期望'{expected_desc}', 实际'{desc}'")
            print()
            break
    
    if not found:
        issues.append(f"图{i}未找到: {filename}")
        print(f"✗ 图{i}: {filename} - 未找到")
        print()

# 输出验证结果
print("=" * 80)
if issues:
    print(f"⚠️  发现 {len(issues)} 个问题:")
    for issue in issues:
        print(f"  - {issue}")
else:
    print("✅ 所有图片和文本匹配正确！")
print("=" * 80)

# 检查图片在markdown中的顺序
print("\n图片在markdown中的顺序:")
for i, (desc, path) in enumerate(case04_images, 1):
    filename = path.split('/')[-1]
    print(f"{i}. {filename} - {desc}")

